﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using log4net;
using System.Reflection;
namespace AnyOffice.general.vehicle.manage
{
    public partial class edit : ModulePage
    {

        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private bool IsImage(string contentType)
        {
            string str;
            return (((str = contentType) != null) && (((str == "image/gif") || (str == "image/jpeg")) || (str == "image/pjpeg")));
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ConfigManager manager = new ConfigManager();
            if (!this.Page.IsPostBack)
            {
                int vehicleId = (Request.QueryString["vehicle_id"] != null) ? int.Parse(Request.QueryString["vehicle_id"]) : 0;
                this.ViewState["VehicleId"] = vehicleId;
                DataTable table = new VehicleDao().SelectByVehicleId(base.Conn, vehicleId);
                if (table.Rows.Count == 0)
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPrompt("无车辆信息", "index1.aspx");
                    return;
                }
                this.txtNumber.Text = (table.Rows[0]["Number"] != DBNull.Value) ? table.Rows[0]["Number"].ToString() : "";
                if (table.Rows[0]["Photo"] != DBNull.Value)
                {
                    this.litUpload.Text = "车辆照片更改：";
                    this.litPhoto.Text = "<a href=\"../photo.aspx?vehicle_id=" + vehicleId.ToString() + "\" target=\"_blank\" title=\"点击查看放大图片\"><img src=\"../photo.aspx?vehicle_id=" + vehicleId.ToString() + "\" width=\"160\" height=\"160\" border=\"0\"></a>";
                }
                this.txtModel.Text = (table.Rows[0]["Model"] != DBNull.Value) ? table.Rows[0]["Model"].ToString() : "";
                this.txtEngineNum.Text = (table.Rows[0]["EngineNum"] != DBNull.Value) ? table.Rows[0]["EngineNum"].ToString() : "";
                if (this.ddlType.Items.FindByValue(table.Rows[0]["Type"].ToString()) != null)
                {
                    this.ddlType.Items.FindByValue(table.Rows[0]["Type"].ToString()).Selected = true;
                }
                this.txtDriver.Text = (table.Rows[0]["Driver"] != DBNull.Value) ? table.Rows[0]["Driver"].ToString() : "";
                this.txtPrice.Text = (table.Rows[0]["Price"] != DBNull.Value) ? table.Rows[0]["Price"].ToString() : "";
                this.txtBuyDate.Text = (table.Rows[0]["BuyDate"] != DBNull.Value) ? Convert.ToDateTime(table.Rows[0]["BuyDate"].ToString()).ToString("yyyy-MM-dd") : "";
                if (this.ddlStatus.Items.FindByValue(table.Rows[0]["Status"].ToString()) != null)
                {
                    this.ddlStatus.Items.FindByValue(table.Rows[0]["Status"].ToString()).Selected = true;
                }
                this.txtRemark.Text = (table.Rows[0]["Remark"] != DBNull.Value) ? table.Rows[0]["Remark"].ToString() : "";
                this.litImageMaxSize.Text = (manager.ImageUploadMaxSize / 1024).ToString();
            }
            if (this.Page.IsPostBack)
            {
                this.pnlMain.Visible = false;
                if (this.filePhoto.PostedFile.ContentLength > 0)
                {
                    if (this.filePhoto.PostedFile.ContentLength > manager.ImageUploadMaxSize)
                    {
                        this.litMessage.Text = base.ShowPrompt("照片大小超过500k！");
                        return;
                    }
                    if (!this.IsImage(this.filePhoto.PostedFile.ContentType))
                    {
                        this.litMessage.Text = base.ShowPrompt("照片格式不正确！");
                        return;
                    }
                }
                VehicleDao dao2 = new VehicleDao();
                VehicleData data = new VehicleData();
                data.VehicleId = Convert.ToInt32(this.ViewState["VehicleId"]);
                data.Number = this.txtNumber.Text;
                if (this.txtModel.Text != string.Empty)
                {
                    data.Model = this.txtModel.Text;
                }
                if (this.txtEngineNum.Text != string.Empty)
                {
                    data.EngineNum = this.txtEngineNum.Text;
                }
                data.Type = this.ddlType.SelectedValue;
                if (this.txtDriver.Text != string.Empty)
                {
                    data.Driver = this.txtDriver.Text;
                }
                if (this.txtPrice.Text != string.Empty)
                {
                    data.Price = this.txtPrice.Text;
                }
                if (this.txtBuyDate.Text != string.Empty)
                {
                    data.BuyDate = Convert.ToDateTime(this.txtBuyDate.Text);
                }
                data.Status = this.ddlStatus.SelectedValue;
                data.Remark = this.txtRemark.Text;
                if (this.filePhoto.PostedFile.ContentLength > 0)
                {
                    int contentLength = this.filePhoto.PostedFile.ContentLength;
                    byte[] buffer = new byte[contentLength];
                    this.filePhoto.PostedFile.InputStream.Read(buffer, 0, contentLength);
                    data.Photo = buffer;
                }
                dao2.Update(base.Conn, data);
                this.litMessage.Text = base.ShowPrompt("修改车辆成功！", "index1.aspx");
            }
        }

 

    }
}
